<?php
/**
 * 合作餐厅管理
 * @author shitou
 *
 */
class OutletAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 添加餐厅
     */
    public function add(){
    	$param = array(
			"upload" => array("single"=>array('thumb','cjthumb')),
    		"before" => "outlet_add_before",
			"after"  => "outlet_after_deal",
    		"return" => true
    	);
    	parent::do_add("Outlet",$param);
    }
    
    /**
     * 修改餐厅
     */
    public function edit(){
    	$param = array(
			"upload" => array("single"=>array('thumb','cjthumb')),
    		"before" => "outlet_add_before",
			"after"  => "outlet_after_deal",
    		"return" => true
    	);
    	parent::do_edit("Outlet",$param);
    }
    /**
     * 餐厅详情
     */
    public function detail(){
    	
    	$id = $this->_req("id");
    	$nofield = $this->_req("nofield");
    	$thumb   = $this->_req("thumb","intval",1);
    	
    	if($id){
    		if($nofield){
    			$data = D("Outlet")->field($nofield,true)->find($id);
    		}else{
    			$data = D("Outlet")->find($id);
    		}
    		
    		if($data['id'] && $thumb==1){
				$data['thumb'] = find_thumb($data['solevar'],'thumb','Outlet');
				$data['cjthumb'] = find_thumb($data['solevar'],'cjthumb','Outlet');
    		}
    		$this->success($data);
    	}else{
    		$this->error();
    	}
    }
    
    /**
     * 餐厅列表
     */
    public function index(){
    	
    	$model = D("Outlet");
    	
    	$cat['city_id']   = $this->_req("city_id","intval",0);
    	$cat['addid']     = $this->_req("addid","intval",0);
    	$cat['role_view'] = $this->_req("role_view","intval",0);
    	//$cat['time_view'] = $this->_req("time_view","intval",0);
    	$map = $model->getRoleOutlet($cat);
    	
    	$desc      = $this->_get("orderDirection","trim","desc");;
    	$order     = $this->_get("orderName","trim","addtime");
    	$pageNum   = $this->_get("pageNum","intval",C('PAGE_NUMS'));
    	$nowPage   = $this->_get("nowPage","intval",1);
    	$where     = $this->_req("where");
    	$package   = $this->_get("package","intval",0);
    	$map       = array();
    	 
    	if($where['keywords']){
    		$map['title'] = array("like","%".$where['keywords']."%");
    	}
    	
    	if($where['type']){
    		$map['type'] = $where['type'];
    	}
    	
    	if($where['level']){
    		$map['level'] = $where['level'];
    	}
    	
    	if($where['area2']){
    		$map['area2'] = $where['area2'];
    	}
    	
    	if($where['area3']){
    		$map['city'] = $where['area3'];
    	}
    	
    	if($where['area4']){
    		$map['area4'] = $where['area4'];
    	}
    	
    	if($where['is_tingye']){
    		$map['is_tingye'] = $where['is_tingye'];
    	}

    	if($where['is_exempt']){
    		$map['is_exempt'] = $where['is_exempt'];
    	}
    	
    	if($where['cuisine']){
    		$map['cuisine'] = array("like",'%'.$where['cuisine'].'%');
    	}
    	if($cat['city_id']){
    		$map['city']  = $cat['city_id'];
    	}
    	
    	$map['isdel'] = 2;
    	
    	$count = $model->where($map)->count();
    	$list  = $model->where($map)->order($order.' '.$desc)
    	->page($nowPage.','.$pageNum)->select();
    		
    	$data  = array("count"=>$count, "list"=>$list);
    	$this->success($data);
    }
    
    
    /**
     * 餐厅列表无分页
     */
    public function view(){
    	
    	$city     = $this->_req("city",    "intval",0);
    	$cuisine  = $this->_req("cuisine", "trim",  "");
    	$city_id  = $this->_req("city_id","intval",0);
		$keywords = $this->_req('keywords','trim','');
		$tingye   = $this->_req('tingye','intval',0);
    	
    	if($city){
    		$con['city'] = $city;
    	}
    	
    	if($city_id){
    		$con['city'] = $city_id;
    	}
    	
    	if($cuisine){
    		$con['cuisine'] = array('like','%'.$cuisine.'%');
    	}
		
		if($keywords){
			$con['title|entitle'] = array('like','%'.$keywords.'%');
		}

		if($tingye){
			$con['is_tingye'] = array('neq',1);
		}
		
    	$con['isdel'] = 2;
    	
    	$data = M("Outlet")->where($con)->order("addtime desc")->select();
    	$this->success($data);
    	
    }
    /**
     * 删除餐厅
     */
    public function del(){
    	
    	$val   = $this->_req("val","trim","");
    	$model = D("Outlet");
    	
    	if($val){
    		$idArr = explode(',',$val);
    		foreach($idArr as $k=>$v){
    			
    			//如果该餐厅已经在地图上标了点，那么要删除这个店
    			$view = $model->find($v);
    			if($view['mapid']){
    				$json  = mapStorge(array("id"=>$view['mapid']),"delete");
    				if($json['status'] === 0){
    					mapAll(true);
    				}
    			}
    			
    			$map['id']    = $v;
    			$map['isdel'] = 1;
    			$data = $model->save($map);
    		}
    	}
    	
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error("请选择您要删除的餐厅");
    	}
    }
    
    /**
     * 所有菜系数据字典
     */
    public function dictCuisine(){
    	$lang    = getLang();
    	$cuisine = getDictByKey("Dict_Cuisine",$lang);
    	
    	$this->success($cuisine);
    }
    /**
     * 所有英文菜系数据字典
     */
    public function endictCuisine(){
    	//$lang    = getLang();
    	$cuisine = getDictByKey("Dict_Cuisine","en-us");

    	$this->success($cuisine);
    }

    
    /**
     * 所有地址数据字典
     */
    public function dictAddress(){
    	
    	$options['zh-cn'] = getDictByKey("Dict_address",'zh-cn');
    	$options['en-us'] = getDictByKey("Dict_address",'en-us');
    	
    	$this->success($options);
    }
    
    /**
     * 添加备注信息点
     */
    public function outletLbs(){
    	
    	$id      = $this->_req("id","intval",0);
    	$address = $this->_req("address","trim",0);
    	$array   = array(); 
    	
    	if($id){
    		$model        = M("Outlet");
    		$array['row'] = $model->find($id);
    	}else{
    		if(empty($address)){
    			$address = C("MAP_DEFAULT_ADDR");
    		}
    		$array['address'] = $address;
    	}
    	$this->success($array);
    }
    
    /**
     * 餐厅批量开业停业
     */
    public function business(){
    	$idStr = $this->_get("val");
    	$type  = $this->_get("type","intval",0);
    	$msg   = "批量操作失败";

    	if($type==1){
    		$map['is_tingye'] = 1;
    		$msg = "批量操作失败，当前没有餐厅需要批量停业";
    	}else{
    		$map['is_tingye'] = 0;
    		$msg = "批量操作失败，当前没有餐厅需要批量开业";
    	}
    	 
    	if($idStr){
    		$idArr = explode(',',$idStr);
    		foreach($idArr as $k=>$v){
    			$map['id']    = $v;
    			$data = M('Outlet')->save($map);
    		}
    	}
    	 
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error($msg);
    	}
    }
    
    /**
     * 批量开启关闭免费配送
     */
    public function distribution(){
    	
    	$idStr = $this->_get("val");
    	$rel   = $this->_get("rel","trim","");
    	$msg   = "批量操作失败";
    	
    	if($rel=="mianfei"){
    		$map['is_exempt'] = 1;
    		$msg = "批量操作失败，当前没有餐厅需要批量开启免费配送";
    	}else{
    		$map['is_exempt'] = 2;
    		$msg = "批量操作失败，当前没有餐厅需要批量关闭免费配送";
    	}
    	
    	if($idStr){
    		$idArr = explode(',',$idStr);
    		foreach($idArr as $k=>$v){
    			$map['id']    = $v;
    			$data = M('Outlet')->save($map);
    		}
    	}
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error($msg);
    	}
    }
   
}